var ns, toast;
var app = new Vue({
    el: '#app',
    data: {
        passModel: 0,
        form: {
            OldPwd: '',
            Password: '',
            RePassword: '',
            action: 'savepassword'
        }
    },
    methods: {
        get_data: function () {
            var that = this;
            ns.post({
                action: 'checkpasswordmodel',
            }, function (succeed, data, err) {
                if (succeed) {
                    that.passModel = data.passModel;
                }
            });
        },
        validatePwd(pwd) {
            var that = this;
            if (that.passModel != 1) {
                return {
                    level: 8,
                    error: ''
                };
            }
            var level = 1;
            if (pwd.length >= 8) {
                level += 3;
            }
            var m = [
                /[0-9]/,
                /[A-Z]/,
                /[a-z]/,
                /[\W_]/
            ];
            for (var i = m.length - 1; i >= 0; i--) {
                if (null !== pwd.match(m[i])) {
                    if (i == 0) {
                        console.log('数字匹配')
                    }
                    if (i == 1) {
                        console.log('大写字母匹配')
                    }
                    if (i == 2) {
                        console.log('小写字母匹配')
                    }
                    if (i == 3) {
                        console.log('特殊字符匹配')
                    }
                    level += 1
                }
            }
            var error = ''
            if (level < 7) {
                error = '密码长度不少于8位，至少包含大写字母、小写字母、数字和特殊字符中的3种';
            }
            return {
                level: level,
                error: error
            };
        },
        dosave: function () {
            var that = this;
            if (that.form.OldPwd == '') {
                api.toast({
                    msg: '请输入原密码',
                    duration: 2000,
                    location: 'bottom'
                });
                return;
            }
            if (that.form.Password == '') {
                api.toast({
                    msg: '请输入新密码',
                    duration: 2000,
                    location: 'bottom'
                });
                return;
            }
            if (that.form.RePassword == '') {
                api.toast({
                    msg: '请确认新密码',
                    duration: 2000,
                    location: 'bottom'
                });
                return;
            }
            if (that.form.RePassword != that.form.Password) {
                api.toast({
                    msg: '两次密码输入不一致',
                    duration: 2000,
                    location: 'bottom'
                });
                return;
            }
            var checkResult = that.validatePwd(that.form.Password);
            if (checkResult.error) {
                api.toast({
                    msg: checkResult.error,
                    duration: 2000,
                    location: 'bottom'
                });
                return;
            }
            toast.loading({
                title: "提交中",
                duration: 2000
            }, function (ret) { });
            ns.post(that.form, function (succeed, data, err) {
                toast.hide();
                if (succeed) {
                    api.toast({
                        msg: '修改成功',
                        duration: 2000,
                        location: 'bottom'
                    });
                    setTimeout(function () {
                        api.closeWin();
                    }, 500);
                } else if (err) {
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            });
        }
    }
});

apiready = function () {
    ns = window.Foresight.Util;
    toast = new auiToast();
    app.get_data();
}